//
//  PhoneManager.m
//  EasyToo
//
//  Created by YuanDee on 13-9-9.
//
//

#import "PhoneManager.h"
#import "CheckPhoneChargeInfoRequest.h"
#import "CheckPhoneChargeInfoResponse.h"
#import "ServerCommunicationManager.h"
#import "ChargeMobilePhoneRequest.h"
#import "ChargeMobilePhoneResponse.h"
@implementation PhoneManager

static PhoneManager *pManager = nil;


+ (PhoneManager*)shareManager
{
    if (pManager == nil) {
        pManager = [[PhoneManager alloc] init];
    }
    return pManager;
}

- (void)checkPhoneCard:(NSString*)phoneNum
{
    CheckPhoneChargeInfoRequest *request = [[CheckPhoneChargeInfoRequest alloc] initWithPhoneNum:phoneNum Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];

}

-(void)chargePhone:(NSString *)phoneNum cardId:(NSString *)cardId cardPrice:(NSString *)cardPrice
{
    ChargeMobilePhoneRequest *request = [[ChargeMobilePhoneRequest alloc] initWithPhoneNum:phoneNum andCardId:cardId andCardPrice:cardPrice Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
}

- (void)onRequestSuccess:(BaseResponse *)response
{
    if ([response isKindOfClass:[CheckPhoneChargeInfoResponse class]])
    {
        [self handlePhoneCardCheckSuccess:(CheckPhoneChargeInfoResponse*)response];
    }
    else if ([response isKindOfClass:[ChargeMobilePhoneResponse class]])
    {
        [self handleChargePhoneSuccess:(ChargeMobilePhoneResponse*)response];
    }


    
}

- (void)onRequestFail:(BaseResponse *)response
{
    if ([response isKindOfClass:[CheckPhoneChargeInfoResponse class]])
    {
        [self.delegate onCheckPhoneChargeInfoFinish:NO cardArea:nil cardType:nil cardInfo:nil];
    }
    
}

- (void)handlePhoneCardCheckSuccess:(CheckPhoneChargeInfoResponse*)response
{
    NSString *area = response.cardArea;
    NSString *type = response.cardType;
    NSMutableArray *infoArray = response.cardInfoArray;
    if (infoArray && area && type)
    {
        [self.delegate onCheckPhoneChargeInfoFinish:YES
                                           cardArea:area
                                           cardType:type
                                           cardInfo:infoArray];
    }
    else
    {
        [self.delegate onCheckPhoneChargeInfoFinish:NO cardArea:nil cardType:nil cardInfo:nil];
    }
}

- (void)handleChargePhoneSuccess:(ChargeMobilePhoneResponse*)response
{
    NSString *tn = response.tn;
    if (tn)
    {
        [self.delegate onChargePhoneFinish:YES tn:tn];
    }
    else
    {
        [self.delegate onChargePhoneFinish:NO tn:nil];
    }
}


@end
